package com.practice.Protobuf.server;

import com.practice.Protobuf.pojo.SubscribeReqProto;
import com.practice.Protobuf.pojo.SubscribeRespProto;
import com.practice.Serialize.pojo.SubscribeResp;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class SubReqServerHandler extends ChannelHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        SubscribeReqProto.SubscribeReq req = (SubscribeReqProto.SubscribeReq) msg;
        if (req.getUserName() != null) {
            System.out.println("server accept req that  " + req.toString());
        }
        ctx.writeAndFlush(resp(req.getSubReqID()));

    }

    private SubscribeRespProto.SubscribeResp resp(int id) {
        SubscribeRespProto.SubscribeResp.Builder builder = SubscribeRespProto.SubscribeResp.newBuilder();
        builder.setSubReqID(id);
        builder.setRespCode(0);
        builder.setDesc("server Received That order, we`ll product it as fast as we can");
        return builder.build();
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
